package com.wfh.mianshiji.config;

import com.wfh.mianshiji.ai.serializer.KryoRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author fenghuanwang
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        // Key 使用 String 序列化
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());

        // Value 使用 Kryo 序列化（注意：这里泛型为 Object，实际使用时建议限定类型）
        // 但 Kryo 需要知道具体类型，因此更推荐为不同业务对象创建专用 RedisTemplate 或使用泛型封装
        template.setValueSerializer(new KryoRedisSerializer<>(Object.class));
        template.setHashValueSerializer(new KryoRedisSerializer<>(Object.class));

        template.afterPropertiesSet();
        return template;
    }


    @Bean
    public RedisTemplate<String, String> chatRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, String> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer()); // 存 JSON 字符串
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}